﻿//创建时间: 2011-8-5
//创建作者: 李城
//功能说明: CMS系统 模板文件管理
//修改: 2011-8-5
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using Newtonsoft.Json;
using TRPOP.CMS.Plugin;


namespace TRPOP.CMS.Admin
{
    public partial class FileSelect : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_strPagePermissionExtraKey = "plugin_TemplateControl";
        }
        #endregion

        #region 页面变量
        protected string m_strRootType = "templete";
        protected string m_strRootTypeName = "模板目录";
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
            }
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelFile_Click(object sender, EventArgs e)
        {
            if (VersionLimit.IsDemo)
            {
                ExecuteJs("alert('Demo版不支持编辑的功能');");
                return;
            }
            string strDeletedFolders = "DeletedFiles";
            string strFile = "/" + ddlRoot.SelectedValue + "/" + hidselpath.Value + "/" + hideSelectName.Value;
            string strRemoveTo = "/" + ddlRoot.SelectedValue + "/" + hidselpath.Value + "/" + strDeletedFolders + "/" + hideSelectName.Value;
            strFile = strFile.Replace("//", "/");
            if (FileUtil.IsExist(Common.Util.GetMapPath(strFile)))
            {
                if (Common.Util.GetMapPath(strFile).Replace("\\" + hideSelectName.Value, "").EndsWith("\\" + strDeletedFolders))
                {
                    FileUtil.DelFile(Common.Util.GetMapPath(strFile));
                    Alert("文件已删除!");
                }
                else
                {
                    if (FileUtil.CopyFile(Common.Util.GetMapPath(strFile), Common.Util.GetMapPath(strRemoveTo), true))
                    {
                        FileUtil.DelFile(Common.Util.GetMapPath(strFile));
                        Alert("文件已删除!");
                    }
                    else
                    {
                        Alert("文件删除失败!");
                    }
                }
            }
            else
            {
                Alert("文件(" + strFile + ")不存在，请确认后再删除");
            }
        }

        /// <summary>
        /// 重新生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReGenerate_Click(object sender, EventArgs e)
        {
            if (VersionLimit.IsDemo)
            {
                ExecuteJs("alert('Demo版不支持编辑的功能');");
                return;
            }

            string strRoot = "/" + ddlRoot.SelectedValue + "/" + hidselpath.Value + "/";
            strRoot = strRoot.Replace("//", "/");
            string strFileName =  hideSelectName.Value;
            string strTemplateFile = Common.Util.GetMapPath(strRoot + strFileName);

            if (FileUtil.IsExist(strTemplateFile))
            {
                TRPOP.CMS.Template.GenerateCS.Access oAccsss = new Template.GenerateCS.Access();
                
                TRPOP.CMS.Template.GenerateCS.TemplateDllPageInfo oCurrentPageInfo = new Template.GenerateCS.TemplateDllPageInfo();
                oCurrentPageInfo.strTemplateCsFilePath = strTemplateFile + ".cs";
                oCurrentPageInfo.strTemplateDLLPath = strTemplateFile + ".dll";
                oCurrentPageInfo.strTemplateFilePath = strTemplateFile;
                oCurrentPageInfo.strCurrentFolder = strRoot;

                if (oAccsss.GenerateCSFile(oCurrentPageInfo) && oCurrentPageInfo.strErrorInfo.IsEmpty())//生成CS文件
                {
                    oAccsss.GenerateDLLFile(oCurrentPageInfo);//生成DLL
                    TRPOP.CMS.Template.ExecuteTemplate.ReMoveOneDllFromCache(oCurrentPageInfo);//从缓存中删除dll信息
                }
                if (!oCurrentPageInfo.strErrorInfo.IsEmpty())
                {
                    Alert("生成错误：" + oCurrentPageInfo.strErrorInfo);
                }
                
            }
            else
            {
                Alert("文件(" + strRoot + strFileName + ")不存在，请确认后再重新生成");
            }
        }
        
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnUpload_Click(object sender, EventArgs e)
        {
            if (VersionLimit.IsDemo)
            {
                ExecuteJs("alert('Demo版不支持编辑的功能');");
                return;
            }
            string strFold = "/" + ddlRoot.SelectedValue + "/" + hidselpath.Value;
             strFold = strFold.Replace("//", "/");
             if (FileUtil.IsExistDirectory(Common.Util.GetMapPath(strFold)) == false)
             {
                 Alert("目录不存在");
                 return;
             }
             if (FileUpLoad.HasFile)
             {
                 string strExtName = "text/html|text/css|text/plain|application/x-javascript|image/|text/xml|application/xml";
                 if ( strExtName.IndexOf(FileUpLoad.PostedFile.ContentType) >= 0 || 
                      FileUpLoad.PostedFile.ContentType.IndexOf("image/") >= 0 ||
                      ( FileUpLoad.PostedFile.ContentType =="application/octet-stream" && FileUpLoad.PostedFile.FileName.EndsWith(".js"))
                    )
                 {
                     FileUpLoad.SaveAs(Common.Util.GetMapPath(strFold) + "\\" + FileUpLoad.FileName);
                 }
                 else
                 {
                     Alert("暂时不支持" + FileUpLoad.PostedFile.ContentType + "格式的文件上传!");
                     return;
                 }
             }
             else
             {
                 Alert("请首先选择文件");
             }

        }
        #endregion

        #region 页面方法

        public void LoadBase()
        {
            switch (m_strRootType.ToLower())
            {
                case "templete"://读取模板目录
                    ddlRoot.DataSource = TempleteConfigInfo.GetTempelteRoot;
                    ddlRoot.DataBind();
                    m_strRootTypeName = "模板目录";
                    break;
                case "root":
                    ddlRoot.Items.Clear();
                    ddlRoot.Items.Add(new ListItem("系统根目录", "/"));
                    ddlRoot.DataBind();
                    break;
                default:
                    break;
            }
        }
        #endregion
    }
}
